package com.imooc.jiangzh.kafka.wechat.service;

import com.alibaba.fastjson.JSONObject;
import com.imooc.jiangzh.kafka.wechat.config.WechatTemplateProperties;
import com.imooc.jiangzh.kafka.wechat.untils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class WechatTemplateServiceImpl implements WechatTemplateService {

    @Autowired
    private WechatTemplateProperties properties;


    /*
    获取目前active为true的调查问卷模板
     */
    @Override
    public WechatTemplateProperties.WechatTemplate getWechatTemplate() {
        List<WechatTemplateProperties.WechatTemplate> templates = properties.getTemplates();
        Optional<WechatTemplateProperties.WechatTemplate> wechatTemplate=
                templates.stream().filter((template)->template.isActive()).findFirst();
        return wechatTemplate.isPresent()? wechatTemplate.get():null;
    }

    @Override
    public void templateReported(JSONObject reportInfo) {
        //kafka producer 将数据推送至kafka topic
        log.info("templateReported:[{}]",reportInfo);

    }

    @Override
    public JSONObject templateStatistics(String templateId) {
        //判断数据结果获取类型
        if(properties.getTemplateResultType()==0){//文件获取
            return FileUtils.readFile2JsonObject(properties.getTemplateResultFilePath()).get();

        }else{
            //DB..

        }
        return null;
    }
}
